<template>
<div class="login-page">
    <van-nav-bar title="账号登录"
    left-arrow
     @click-left="onClickLeft"/>
    <van-form @submit="onSubmit">
    <van-field
      v-model="username"
      name="username"
      placeholder="请输入账号"
      :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '用户名至少包含5个字符' }
        ]"
    />
    <van-field
      v-model="password"
      type="password"
      name="password"
      placeholder="请输入密码"
      :rules="[{ required: true, message: '请填写密码' },
      { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' }
    ]"
    />
    <div style="margin: 16px;">
      <van-button block class="login" native-type="submit">登录</van-button>
    </div>
  </van-form>
  <router-link class="link" to="/register">还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'// 引入请求接口
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (val) {
      console.log('onSubmit', val)
      const res = await login(val)
      // console.log(res)
      setToken(res.body.token)
      this.$toast.success('账号登录成功')
      this.$router.push('/my')
    },
    onClickLeft () {
      this.$router.push('/my')
    }
  }
}
</script>

<style lang="less" scoped>

.link {
  color: #999;
  font-size: 15px;
  padding:15px 110px;
  float: right;
}
.van-nav-bar__content .van-nav-bar__title .van-ellipsis{
  color: aliceblue;
  background-color: #1cb676;
}
.login{
  color: aliceblue;
  background-color: #1cb676;
}
</style>
